//
//  LLGWelComeViewController.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/10.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class LLGWelComeViewController: UIViewController {

    // MARK: --背景图片
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // MARK: --头像
    private lazy var iconImageView: UIImageView = {
        
        let imageView = UIImageView()
        //切圆角
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        if let avatar_large = LLGUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
        
            imageView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return imageView
    }()
    
    // MARK: --欢迎文字
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        if let name = LLGUserAccountViewModel.sharedUserAccount.userAccount?.name {
        
            label.text = "欢迎回来,\(name)";
        } else {
            label.text = "欢迎回来"
        }
        
        
        
        return label
    }()
    
    override func loadView() {
        
        //背景图片
        view = backgroundImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    // MARK: --页面已经显示
    override func viewDidAppear(animated: Bool) {
        super.viewDidDisappear(animated)
        //开启动画
        startAnimation()
    }

    // MARK: --设置界面
    private func setupUI() {
    
        view.addSubview(iconImageView)
        view.addSubview(messageLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(250)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
        }
    }
    
    // MARK: --头像动画
    private func startAnimation() {
    
        //透明度
        messageLabel.alpha = 0
        
        //更新头像布局
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        //动画实现
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    //透明度为1
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                       
                        //发送通知,切换根控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(switchRootViewController, object: nil)
                })
                
        }
    }

    

}
